package leetcode;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-18 00:07
 **/
public class LeetCode539 {

    public int findMinDifference(List<String> timePoints) {
        int n = timePoints.size();
        //由鸽巢原理可知，如果 timePoints 的长度超过 1440，那么必然会有两个相同的时间，此时可以直接返回 0。
        if (n > 1440) {
            return 0;
        }
        Collections.sort(timePoints);
        String first = timePoints.get(0);
        int zero = Integer.parseInt(first.substring(0, 2)) * 60 + Integer.parseInt(first.substring(3));;
        int pre = zero;
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < n; i++) {
            String timePoint = timePoints.get(i);
            int cur = Integer.parseInt(timePoint.substring(0, 2)) * 60 + Integer.parseInt(timePoint.substring(3));
            min = Math.min(cur - pre, min);
            pre = cur;
        }

        min = Math.min(zero + 1440 - pre, min);
        return min;
    }

    public static void main(String[] args) {
        LeetCode539 code = new LeetCode539();
        code.findMinDifference(Arrays.asList("23:59","00:00"));
    }

}
